#coding=utf-8



from CCPRestSDK import REST
import ConfigParser

#主帐号
accountSid= '8aaf07085fe2d98c015ff2a1fa380661'

#主帐号Token
accountToken= '8107cec9a9134abf8e692a83e9ba83dd'

#应用Id
appId='8aaf07085fe2d98c015ff2a1fa8f0667'

#请求地址，格式如下，不需要写http://
serverIP='app.cloopen.com'

#请求端口 
serverPort='8883'

#REST版本号
softVersion='2013-12-26'

  # 发送模板短信
  # @param to 手机号码
  # @param datas 内容数据 格式为数组 例如：{'12','34'}，如不需替换请填 ''
  # @param $tempId 模板Id


'''因为如果每次调用都初始化REST_SDK感觉不好,所以对其进行优化
    怎么优化?
    利用单例模式
'''

class SendSMS(object):
    '''单例模式实现发送短信'''
    _instance = None

    def __new__(cls):
        # super(SendSMS, cls).__new__(*args, **kwargs)

        if not cls._instance:
            obj = super(SendSMS, cls).__new__(cls)
            # 初始化REST SDK
            obj.rest = REST(serverIP, serverPort, softVersion)
            obj.rest.setAccount(accountSid, accountToken)
            obj.rest.setAppId(appId)

            # result = rest.sendTemplateSMS(to, datas, tempId)
            cls._instance = obj

        return cls._instance

    def send_sms(self,to,datas,tempId):
        result = self.rest.sendTemplateSMS(to,datas,tempId)
        print(result)
        if result.get('statusCode') == '000000':
            # 发送成功,返回0
            return 0
        else:
            # 不成功,返回1
            # print(result.get('statusCode'))
            return -1




#sendTemplateSMS(手机号码,内容数据,模板Id)
# sendTemplateSMS('18649715651',{'1234',u'2分钟'},1)

if __name__ == '__main__':
    s = SendSMS()
    s1 = SendSMS()
    print(id(s))
    print(id(s1))
    r = s.send_sms('18649715651',{'1234','2分钟'},1)
    print(r)